package com.blx.utils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;


/**   
 * @Title: CookieUtil.java
 * @Package com.sohu.sns.util.tools
 * @Description: TODO
 * @author libo 
 * @date 2012-11-5 上午10:30:01
 * @version V1.0   
 */
public class CookieUtil {
	/**
	 * 加密后会出现等号，由于等号会被request截掉，加密后替换成该符号
	 */
	private final static String symbols = "%yicidh%";
	 /** 
     * 给response add cookie  
     * @param key 
     * @param value 
     * @param maxAge 
     * @return 
     * @throws UnsupportedEncodingException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeyException 
     */  
    public final static void addCookie(HttpServletResponse response,String key, String value,String path, int maxAge)  
            throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {  
        Cookie cookie = new Cookie(md5(key), encrypt(value));  
        cookie.setMaxAge(maxAge);  
        cookie.setPath("/");  
        if(null != path){
        	cookie.setPath(path);
        }
        response.addCookie(cookie);
    }  
  
    /** 
     * 根据key获取cookie  
     * @param key 
     * @param cookie 
     * @return 
     * @throws Base64DecodingException 
     * @throws BadPaddingException 
     * @throws IllegalBlockSizeException 
     * @throws UnsupportedEncodingException 
     * @throws NoSuchPaddingException 
     * @throws NoSuchAlgorithmException 
     * @throws InvalidKeyException 
     * @throws UnsupportedEncodingException 
     */  
    public final static String getCookieByName(HttpServletRequest request,String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException{  
    	Map<String, Cookie> cookieMap = getCookieMap(request);
    	key = md5(key);
        if(cookieMap.containsKey(key)){
            Cookie cookie = (Cookie)cookieMap.get(key);
            return decode(cookie.getValue());
        }else{
            return null;
        }
    }  
    
    /**
     * 取出request中所有cookie并放入map
    * @Description: 
    * @param request
    * @return    
    * @return Map<String,Cookie>   
    * @throws
     */
    public final static Map<String,Cookie> getCookieMap(HttpServletRequest request){
    	 Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
         Cookie[] cookies = request.getCookies();
         if(cookies != null){
	         for(Cookie cookie : cookies){
	        	 cookieMap.put(cookie.getName(), cookie);
	         }
         }
         return cookieMap;
    }
    
    /**
	 * 加密cookie
     * @throws BadPaddingException 
     * @throws IllegalBlockSizeException 
     * @throws UnsupportedEncodingException 
     * @throws NoSuchPaddingException 
     * @throws NoSuchAlgorithmException 
     * @throws InvalidKeyException 
	 */
	private final static String encrypt(String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException{
		return Passport.encrypt(value).replace("=", symbols);//Base64.encode(value.getBytes());
	}
	/**
	 * key值加密
	 */
	private final static String md5(String key){
		return DigestUtils.md5Hex(key);
	}
    /**
     * 解密
     * @throws Base64DecodingException 
     * @throws BadPaddingException 
     * @throws IllegalBlockSizeException 
     * @throws UnsupportedEncodingException 
     * @throws NoSuchPaddingException 
     * @throws NoSuchAlgorithmException 
     * @throws InvalidKeyException 
     */
	private final static String decode(String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException{
		value = value.replace(symbols, "=");
		return (new String(Passport.decrypt(value)));
	}
	
}
